package com.adou.p2p.fiter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.adou.p2p.model.UserValue;

public class LoginFilter implements Filter {

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException,
			ServletException {
		// 判断用户是否登录，只需要判断session中是否有user
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;

		UserValue user = (UserValue) request.getSession().getAttribute("user");

		if (user != null) {
			// 登录
			chain.doFilter(request, response);
		} else {
			// 未登录
			response.sendRedirect(request.getContextPath() + "/login.jsp");
		}
	}

}
